package net.starwifi.duobao.utils.times;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Utils-Date.
 *
 * @author <a href="http://ruo.whosenet.com">ruo.whosenet.com</>
 * @version 1.0
 */
public class DateTool {

    public static long getDaysBetween(Date fromDate,Date toDate){
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(fromDate);
        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
        fromCalendar.set(Calendar.MINUTE, 0);
        fromCalendar.set(Calendar.SECOND, 0);
        fromCalendar.set(Calendar.MILLISECOND, 0);

        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(toDate);
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);
        toCalendar.set(Calendar.MINUTE, 0);
        toCalendar.set(Calendar.SECOND, 0);
        toCalendar.set(Calendar.MILLISECOND, 0);
        return (toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000l * 60 * 60 * 24);
    }

    public static int getMonthsBetween(Date date1, Date date2){
        int iMonth = 0;
        int flag = 0;
        try{
            Calendar fromCalendar1 = Calendar.getInstance();
            fromCalendar1.setTime(date1);

            Calendar fromCalendar2 = Calendar.getInstance();
            fromCalendar2.setTime(date2);

            if (fromCalendar2.equals(fromCalendar1))
                return 0;
            if (fromCalendar1.after(fromCalendar2)){
                Calendar temp = fromCalendar1;
                fromCalendar1 = fromCalendar2;
                fromCalendar2 = temp;
            }
            if (fromCalendar2.get(Calendar.DAY_OF_MONTH) < fromCalendar1.get(Calendar.DAY_OF_MONTH))
                flag = 1;

            if (fromCalendar2.get(Calendar.YEAR) > fromCalendar1.get(Calendar.YEAR))
                iMonth = ((fromCalendar2.get(Calendar.YEAR) - fromCalendar1.get(Calendar.YEAR))
                        * 12 + fromCalendar2.get(Calendar.MONTH) - flag)
                        - fromCalendar1.get(Calendar.MONTH);
            else
                iMonth = fromCalendar2.get(Calendar.MONTH)
                        - fromCalendar1.get(Calendar.MONTH) - flag;

        } catch (Exception e){
            e.printStackTrace();
        }
        return iMonth;
    }

    /**
     * 是否只有日期数据
     *
     * @param date
     * @return
     */
    public static boolean onlyDateData(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int h = calendar.get(Calendar.HOUR_OF_DAY);
        int m = calendar.get(Calendar.MINUTE);
        int s = calendar.get(Calendar.SECOND);
        return h==0 && m==0 && s==0;
    }

    /**
     * 在指定日期增加天数
     *
     * @param date
     * @param amount
     * @return
     */
    public static Date addDays(Date date, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, amount);
        return c.getTime();
    }




    /**
     * 设置 时，分,秒
     *
     * @param date
     * @param h
     * @param m
     * @param s
     */
    public static Date setHms(Date date, int h, int m, int s) {
        if(date==null) return null;
        Calendar c = Calendar.getInstance();
        c.setLenient(false);
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY,h);
        c.set(Calendar.MINUTE,m);
        c.set(Calendar.SECOND,s);
        return c.getTime();
    }

    /**
     * 根据指定的日期表达式解析
     *
     * @param dateStr
     * @param datePattern
     * @return
     */
    public static Date parseDate(String dateStr,String datePattern) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
            return dateFormat.parse(dateStr);
        } catch (ParseException e) {
            throw new IllegalArgumentException("Could not parse date: " + dateStr,e);
        }
    }

    public static Date autoParseDate(String dateStr){
        String datePattern = null;
        if(dateStr.matches("[0-9]{4}-[0-9]{2}-[0-9]{2}")){
            datePattern = "yyyy-MM-dd";
        }else if(dateStr.matches("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}")){
            datePattern = "yyyy-MM-dd HH:mm";
        }else if(dateStr.matches("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}")){
            datePattern = "yyyy-MM-dd HH:mm:ss";
        }else{
            throw new IllegalArgumentException("Could not parse date:" + dateStr +", date format is error ");
        }

        return parseDate(dateStr,datePattern);
    }
}
